<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if ($f == 'check' || $f == 'uncheck' || $f == 'delete' || $f == 'newstatus' || $f == 'newproperty' || $f == 'move' || $f == 'update') {
	$f ='operation' ;
}
if(isset($_G['usergroup']['access']["pot$f"]) && !ican("pot$f")){
	msg('没有权限');
}
$m = new PointModule;
$m->$f();
class PointModule
{
	function index(){
		global $_G;
		if(getGP('pointsubmit','P')){
			$set=getGP('point','P','array');
			$points=array(
				'pointsetting'=>encrypt(serialize($set)),
				'pointuse'=>getGP('used','P'),
				'pointratio' => getGP('ratio','P','int'),
				'pointmin'=>getGP('minpoint','P','int')
			);
			if($points['pointuse']!='0' && !$set[$points['pointuse']]['use']){
				msg('设置失败! 交易积分所使用的类型'.$points['pointuse'].'并没有启用');
			}
			foreach ($points as $key=>$val){
				if ($_G['db']->fetch_one_array("SELECT * FROM ".DB_PREFIX."settings WHERE `type` = 'point' AND `name` = '$key'")) {
					$_G['db']->query("UPDATE ".DB_PREFIX."settings SET `value` = '$val' WHERE `type` = 'point' AND `name` = '$key'");
				} else {
					$_G['db']->query("INSERT INTO ".DB_PREFIX."settings (`name`, `value`, `type`) VALUES ('$key', '$val', 'point')");
				}
			}
			recache('settings');
			msg('保存成功，你可能需要刷新一下缓存！');
		}else{
			$point=$_G['settings']['point'];
			$points = unserialize(decrypt($point['pointsetting']));
			if(empty($points)){
				$points = array('point1'=>'point1','point2'=>'point2','point3'=>'point3');
			}else{
				ksort($points);
			}
			
		}
		include admintemplate('point');
	}
	
	function setting(){
		global $_G;
		$points=unserialize(decrypt($_G['settings']['point']['pointsetting']));
		$result = $_G['db']->fetch_all("SELECT * FROM".table('point_rule')." ORDER BY rid DESC");
		include admintemplate('point_setting');
	}
	function edit(){
		global $_G;
		if(getGP('pointsubmit','P')){
			$point = getGP('point','P','array');
			$rid = getGP('rid','P','int');
			if($rid){
				$_G['db']->update('point_rule',$point,array('rid'=>$rid));
			}
			msg("修改成功","admin.php?m=point&f=setting");
		}else{
			$points=unserialize(decrypt($_G['settings']['point']['pointsetting']));
			$rid = getGP('id','G','int');
			if(!$rid){
				msg("参数错误");
			}
			$point=$_G['db']->fetch_one_array("SELECT * FROM".table('point_rule')."WHERE rid=".$rid);
		}
		include admintemplate('point_edit');
	}
}